package belf.migrate.core.jdbc;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 从jdbc url中提取host和port
 */
@Slf4j
public class JDBCUrlTool {
    private static final Pattern JDBC_URL_PATTERN = Pattern.compile("(jdbc:[^:]+)://([^:]+):(\\d+)(.*)");

    public HostPort parseHostPort(String jdbcUrl) {
        Matcher matcher = JDBC_URL_PATTERN.matcher(jdbcUrl);
        if (matcher.matches()) {
            String host = matcher.group(2);
            int port = Integer.parseInt(matcher.group(3));
            return new HostPort(host, port);
        } else {
            log.error("Invalid JDBC URL format: {}", jdbcUrl);
        }
        return null;

    }

    public class HostPort {
        public HostPort(String host, int port) {
            this.host = host;
            this.port = port;
        }

        public String host;
        public int port;
    }
}
